<template>
  <div class="cu-common-layout">
    <div class="cu-common-layout-center">
      <el-row class="cu-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="标题">
              <el-input v-model="query.title" placeholder="标题" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                native-type="submit">{{ $t("common.search") }}</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">{{$t('common.reset')}}</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="cu-common-layout-main cu-flex-main">
        <div class="cu-common-head">
          <div>
            <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()" v-has="'btn_add'">新增</el-button>
          </div>
          <div class="cu-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh cu-common-head-icon" :underline="false" @click="reset()" />
            </el-tooltip>
            <screenfull isContainer />
          </div>
        </div>
        <custom-table v-loading="listLoading" :data="list">
          <el-table-column label="推送品牌" prop="brand" align="left" v-if="custom.hasP('brand')" width="200y">
            <template slot-scope="scope">{{ scope.row.brand | dynamicCodeText(brandOptions) }}</template>
          </el-table-column>
          <el-table-column prop="title" label="标题" align="left" v-if="custom.hasP('title')" width="200" />
          <el-table-column prop="content" label="内容" align="left" v-if="custom.hasP('content')" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-link @click="readInfo(scope.row.content)" style="font-size: 12px">
                {{ scope.row.content }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="createUserName" label="创建用户" width="80" align="left"
            v-if="custom.hasP('createUserName')" />
          <el-table-column prop="createTime" label="创建时间" align="center" v-if="custom.hasP('createTime')" width="120"
            :formatter="custom.tableDateTimeFormat" />
          <el-table-column prop="updateUserName" label="修改用户" width="80" align="left"
            v-if="custom.hasP('updateUserName')" />
          <el-table-column prop="" label="修改时间" align="center" v-if="custom.hasP('updateTime')" width="120"
            :formatter="custom.tableDateTimeFormat" />
          <el-table-column label="操作" align="center" fixed="right" width="100">
            <template slot-scope="scope">
              <table-opts @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)">
              </table-opts>
            </template>
          </el-table-column>
        </custom-table>
        <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
          @pagination="initData" />
      </div>
    </div>
    <custom-form v-if="formVisible" ref="CustomForm" @refresh="refresh" />
    <detail v-show="detailVisible" ref="detail" @close="detailVisible = false" />
  </div>
</template>
<script>
import request from "@/utils/request";
import CustomForm from "./Form";
import Detail from "./Detail";
export default {
  components: { CustomForm, Detail },
  data () {
    return {
      query: {
        title: undefined,
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "createTime",
      },
      formVisible: false,
      detailVisible: false,
      columnList: [
        { prop: "title", label: "标题" },
        { prop: "content", label: "内容" },
        { prop: "createUserName", label: "创建用户姓名" },
        { prop: "createTime", label: "创建时间" },
        { prop: "updateUserName", label: "修改用户名称" },
        { prop: "updateTime", label: "修改时间" },
      ],
      brandOptions: [],
    };
  },
  computed: {},
  created () {
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
    this.initData();
  },
  methods: {
    initData () {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
      };
      let query = {};
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join();
        } else {
          query[key] = _query[key];
        }
      }
      request({
        url: `/api/campus/notice`,
        method: "GET",
        data: query,
      }).then((res) => {
        this.list = res.data.list;
        this.total = res.data.pagination.total;
        this.listLoading = false;
      });
    },

    readInfo (bodyText) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.detail.init(bodyText);
      });
    },
    handleDel (id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/campus/notice/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => { });
    },
    addOrUpdateHandle (id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail);
      });
    },
    search () {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "createTime",
      };
      this.initData();
    },
    refresh (isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset () {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "createTime",
      };
      this.initData();
    },
  },
};
</script>
